#include <unistd.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include "Bitmap.h"
#include "fblib.h"

struct Circle {
	float x;
	float y;
	float radius;
	int vx;
	int vy;
} circle;

extern long location(struct fb_dev* const dev, int x, int y);
int main(int argc, char** argv)
{
	struct fb_dev* dev = fb_open("/dev/fb0", O_RDWR);
	if(dev == NULL) {
		printf("Error in open fb_dev\n");
		return -1;
	}
	fb_info(dev);
#define RED32 (0xFF<<16)
#define GREEN32 (0xFF<<8)
#define BLUE32 (0xFF<<0)
	while(1) {
		fb_fill_rect(dev, 0, 0, 600, 400, RGB32(255,0,0));
		fb_repaint_all(dev);
		getchar();
		fb_fill_rect(dev, 0, 0, 600, 400, RGB32(0,255,0));
		fb_repaint_all(dev);
		getchar();
		fb_fill_rect(dev, 0, 0, 600, 400, RGB32(0,0,255));
		fb_repaint_all(dev);
		getchar();
	}

	/*
	Bitmap bitmap;
	char* names[] = { "1.bmp", "2.bmp", "3.bmp", "4.bmp", "minions.bmp", "minion.bmp", "tree.bmp", "player.bmp" };

	Rect des_rect;
	des_rect.x = 0;
	des_rect.y = 0;
	des_rect.width = dev->xres;
	des_rect.height = dev->yres;
	Rect res_rect;

	srand(time(NULL));
	circle.x = 10;
	circle.y = 10;
	circle.radius = 5;
	circle.vx = rand() % 40;
	circle.vy = rand() % 40;
	printf("vx : %d, vy : %d\n", circle.vx, circle.vy);
	getchar();

	if(0 == bitmap_init(&bitmap, "3.bmp")) {
		printf("CAn't bitmap_init\n");
		return -1;
	}
	res_rect.x = 0;
	res_rect.y = 0;
	res_rect.width = bitmap.xres;
	res_rect.height = bitmap.yres;

	int target_x;
	int target_y;
	int tox = 0;
	int add = 1;
	int n = 0;
	while(1) {
		//fb_draw_bitmap(dev, des_rect, res_rect, &bitmap, DrawInvertY);
		fb_fill_rect(dev, 0, 0, dev->xres, dev->yres, RGB16(0,0,0));
		fb_draw_bitmap(dev, des_rect, res_rect, &bitmap, DrawInvertY);
		target_x = circle.x + circle.vx;
		if(target_x < circle.radius) {
			circle.vx = 10;
		} else if(target_x > dev->xres - circle.radius) {
			circle.vx = -10;
		}

		target_y = circle.y + circle.vy;
		if(target_y < 0) {
			circle.vy = 10;
		} else if(target_y >= dev->yres - circle.radius) {
			circle.vy = -10;
		}

		int color = *(unsigned short*)(dev->double_buffer + location(dev, target_x, circle.y));
		if(color != RGB16(0xFF,0xFF,0xFF)) {
			circle.vx *= -1;
			target_x = circle.x;
		}

		color = *(unsigned short*)(dev->double_buffer + location(dev, circle.x, target_y));
		if(color != RGB16(0xFF,0xFF,0xFF)) {
			circle.vy *= -1;
			target_y = circle.y;
		}

		circle.x = target_x;
		circle.y = target_y;
		fb_fill_circle(dev, circle.x, circle.y, circle.radius, BLACK);

		fb_repaint_all(dev);
		usleep(10000);
		n++;
		if(n % dev->xres*3/5 == 0) {
			add = -add;
		}
		tox += add;
		res_rect.x = fromx;
		res_rect.y = fromy;
		//	getchar();
	}
	bitmap_destroy(&bitmap);
	*/


	fb_close(dev);

	return 0;
}
/*  
	long location(struct fb_dev* const dev, int x, int y) 
	{
	if(x < 0 || y < 0)
	return 0;
	long temp;
	temp = (x+dev->fb_var_screeninfo.xoffset) * dev->bits_per_pixel/8 +
	(y+dev->fb_var_screeninfo.yoffset) * dev->fb_fix_screeninfo.line_length;
	if(temp > dev->buffer_size)
	return 0;
	return temp;
	}
	*/
